﻿//*************************************************************************************************
// ValidationRuleInnerText.cs
// Owner: Herodotos Herodotou (t-herohe)
//
// A validation rule used to validate the inner text from an html tag
//
// Copyright(c) Microsoft Corporation, 2008
//*************************************************************************************************

using System;
using System.ComponentModel;
using System.Globalization;
using WebAndLoadTestPluginsAndRules.Properties;

using Microsoft.VisualStudio.TestTools.WebTesting;

namespace WebAndLoadTestPluginsAndRules
{
    [LocalizableDisplayName("ValidationRuleNameTagInnerText")]
    [LocalizableDescription("ValidationRuleDescriptionTagInnerText")]
    public class ValidationRuleInnerText : ValidationRule
    {
        // ****************************************************************************
        // Overriden method
        // ****************************************************************************

        /// <summary>
        /// Validate the Inner Text of a particular html tag
        /// </summary>
        public override void Validate(object sender, ValidationEventArgs e)
        {
            int i = 0;
            HtmlTagInnerTextParser parser = new HtmlTagInnerTextParser(e.Response.BodyString);
            foreach (InnerText innerText in parser.GetInnerTextForHtmlTags(TagName, AttributeName, AttributeValue,
                                                                           HasClosingTags, RemoveInnerTags, CollapseWhiteSpace))
            {
                if (Index == -1 || Index == i)
                {
                    if (IgnoreCase == true)
                    {
                        e.IsValid = string.Equals(innerText.Value, ExpectedInnerText, StringComparison.OrdinalIgnoreCase);
                    }
                    else
                    {
                        e.IsValid = string.Equals(innerText.Value, ExpectedInnerText, StringComparison.Ordinal);
                    }

                    if (e.IsValid)
                    {
                        return;
                    }
                }
                ++i;
            }

            // Unable to find the tag
            e.IsValid = false;
            if (!e.Response.IsHtml)
            {
                e.Message = string.Format(CultureInfo.InvariantCulture,
                    Resources.ErrorResponseNotHtmlForValidationRules);
            }
            else
            {
                e.Message = string.Format(CultureInfo.InvariantCulture,
                    Resources.ValidationErrorInnerTextNotFound, ExpectedInnerText, TagName);
            }
        }


        // ****************************************************************************
        // Public Properties
        // ****************************************************************************

        [LocalizableDisplayName("PropertyNameTagName")]
        [LocalizableDescription("PropertyDescriptionTagName")]
        public string TagName
        {
            get { return m_tagName; }
            set { m_tagName = value; }
        }

        [LocalizableDisplayName("PropertyNameAttributeName")]
        [LocalizableDescription("PropertyDescriptionAttributeName")]
        public string AttributeName
        {
            get { return m_attributeName; }
            set { m_attributeName = (value.Length == 0) ? null : value; }
        }

        [LocalizableDisplayName("PropertyNameAttributeValue")]
        [LocalizableDescription("PropertyDescriptionAttributeValue")]
        public string AttributeValue
        {
            get { return m_attributeValue; }
            set { m_attributeValue = (value.Length == 0) ? null : value; }
        }

        [LocalizableDisplayName("PropertyNameExpectedInnerText")]
        [LocalizableDescription("PropertyDescriptionExpectedInnerText")]
        public string ExpectedInnerText
        {
            get { return m_expectedInnerText; }
            set { m_expectedInnerText = value; }
        }

        [LocalizableDisplayName("PropertyNameRemoveInnerTags")]
        [LocalizableDescription("PropertyDescriptionRemoveInnerTags")]
        [DefaultValue(true)]
        public bool RemoveInnerTags
        {
            get { return m_removeInnerTags; }
            set { m_removeInnerTags = value; }
        }

        [LocalizableDisplayName("PropertyNameClosingTag")]
        [LocalizableDescription("PropertyDescriptionClosingTag")]
        [DefaultValue(true)]
        public bool HasClosingTags
        {
            get { return m_hasClosingTag; }
            set { m_hasClosingTag = value; }
        }

        [LocalizableDisplayName("PropertyNameCollapseExtraWhiteSpace")]
        [LocalizableDescription("PropertyDescriptionCollapseExtraWhiteSpace")]
        [DefaultValue(true)]
        public bool CollapseWhiteSpace
        {
            get { return m_collapseWhiteSpace; }
            set { m_collapseWhiteSpace = value; }
        }

        [LocalizableDisplayName("PropertyNameIndex")]
        [LocalizableDescription("PropertyDescriptionIndex")]
        [DefaultValue(-1)]
        public int Index
        {
            get { return m_index; }
            set { m_index = value; }
        }

        [LocalizableDisplayName("PropertyNameIgnoreCase")]
        [LocalizableDescription("PropertyDescriptionIgnoreCase")]
        [DefaultValue(true)]
        public bool IgnoreCase
        {
            get { return m_ignoreCase; }
            set { m_ignoreCase = value; }
        }

        // ************************************************************************
        // Private Member Variables.
        // ************************************************************************

        private string m_tagName;
        private string m_attributeName;
        private string m_attributeValue;
        private string m_expectedInnerText;

        private bool m_hasClosingTag;
        private bool m_removeInnerTags;
        private bool m_collapseWhiteSpace;
        private int m_index;
        private bool m_ignoreCase;
    }
}
